home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 3: CDPD 3
/
Almathera Ten on Ten - Disc 3: CDPD3.iso
/
scope
/
051-075
/
scopedisk54
/
fold
/
fold.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-18
|
2KB
|
83 lines
#include <stdio.h>
#include <fcntl.h>
FILE *infile;
char buf[8*BUFSIZ];
int lin_len=80;
void
main( argc, argv )
int argc;
char *argv[];
{
int n;
int start=1;
if( *argv[1] == '-' )
{
lin_len = atoi( argv[1] ) * -1;
start ++;
}
if( lin_len <= 0 ) lin_len = 80;
if( argc <= start )
{
fprintf( stderr, "Breaks lines at space or tab, creating new line\nUsage: %s [>outfile] [-lin_length] file [file ...]\n", argv[0] );
exit( 0 );
}
for( ;start < argc; start ++ )
{
char *p;
p = argv[start];
if( (infile = fopen( p, "r" )) == NULL )
{
perror( p );
continue;
}
while( fgets( buf, BUFSIZ, infile ) != NULL )
{
buf[strlen(buf)-1] = 0;
process( buf );
puts(buf);
fflush( stdout );
}
fclose( infile );
}
}
process( p )
char p[];
{
int tablen = 8;
int i;
int l = 0;
int s = 0;
for ( i = 0; i < BUFSIZ && p[i]; i++ )
{
if ( p[i] == '\t' )
l = ((l + tablen)/tablen)*tablen;
else
l ++;
if ( l > lin_len )
{
for (l = i; l > s; l--)
if (p[l] == ' ' || p[l] == '\t')
{
p[l] = '\n';
i = l+1;
s = i;
l = 0;
break;
}
}
}
}